#!/bin/bash
source ../base_model.sh

########################################### set output path ################################
cur_ms_source_path=$(pwd)
dstPosition=$(pwd)/runEnv
############################################### end ########################################


########################################## copy runEnv path ################################
rm -rf ${dstPosition}
mkdir -p ${dstPosition}

cp ${cur_ms_source_path}/mindspore/lite/build/src/libmindspore-lite.so ${dstPosition}
cp ${cur_ms_source_path}/mindspore/lite/build/tools/benchmark/benchmark ${dstPosition}
cp ${cur_ms_source_path}/mindspore/lite/build/_deps/glog-src/_build/libglog.so.0 ${dstPosition}
cp ${cur_model_pos}/* ${dstPosition}
############################################### end ########################################


ms_options=""
function AddOption() {
    ms_options="$1 $2"
}

function DoBenchmarkCMD() {
    benchmark_cmd_file=$1

    echo "export LD_LIBRARY_PATH=${dstPosition}" > ${dstPosition}/${benchmark_cmd_file}.sh
    echo "./benchmark ${ms_options}" >> ${dstPosition}/${benchmark_cmd_file}.sh
    bash ${dstPosition}/${benchmark_cmd_file}.sh 2>&1 | tee ${dstPosition}/${benchmark_cmd_file}.log

    echo ""
    echo ""
    echo "############################# Current Run CMD #########################################"
    cat ${dstPosition}/${benchmark_cmd_file}.sh
}

function run_benchmark_accuracy_demo() {
    cd ${dstPosition}
    chmod +777 ${cur_ms_model_file}
    dump_output=${dstPosition}/dump_output/
    rm -rf ${dump_output}
    mkdir ${dump_output}

    cur_ms_model_input_file=${cur_model_input_file}_0
    for ((i=1; i<$ms_model_input_num;i++))
    do
        cur_ms_model_input_file=${cur_ms_model_input_file},${cur_model_input_file}_${i}
    done

    device=$1
    enableFp16=$2
    numThreads=$3

    ms_options=""
    AddOption "${ms_options}" "--modelFile=${cur_ms_model_file}"
    AddOption "${ms_options}" "--inDataFile=${cur_ms_model_input_file}"
    AddOption "${ms_options}" "--benchmarkDataFile=${cur_model_output_file}"
    AddOption "${ms_options}" "--benchmarkDataType=${ms_benchmarkDataType}"
    AddOption "${ms_options}" "--device=${device}"
    AddOption "${ms_options}" "--enableFp16=${enableFp16}"
    AddOption "${ms_options}" "--numThreads=${numThreads}"
    AddOption "${ms_options}" "${ms_benchmark_append}"

    DoBenchmarkCMD "do_run_accuracy_cmd"
    print_info "end -> accuracy, model = ${cur_model_file}, device = ${device} , enableFp16 = ${enableFp16}"
}
run_benchmark_accuracy_demo CPU false 8


function run_benchmark_performance_demo() {
    cd ${dstPosition}
    chmod +777 ${cur_ms_model_file}
    device=$1
    enableFp16=$2
    numThreads=$3
    loopCount=10
    # ms_benchmark_append=" --inputShapes=${4},1:${4},1:${4},217:${4},1 "
    # ms_benchmark_append=" --inputShapes=${4},94:${4},94 "

    ms_options=""
    AddOption "${ms_options}" "--modelFile=${cur_ms_model_file}"
    AddOption "${ms_options}" "--loopCount=${loopCount}"
    AddOption "${ms_options}" "--timeProfiling=false"
    AddOption "${ms_options}" "--device=${device}"
    AddOption "${ms_options}" "--enableFp16=${enableFp16}"
    AddOption "${ms_options}" "--numThreads=${numThreads}"
    AddOption "${ms_options}" "${ms_benchmark_append}"

    DoBenchmarkCMD "do_run_performance_cmd"
    print_info "end -> performance, model = ${cur_model_file}, device = ${device} , enableFp16 = ${enableFp16}"
}

run_benchmark_performance_demo CPU false 16
